//
//  InstallationPointsTableViewController.m
//  GoodWayClient
//
//  Created by HuiDong on 15/12/3.
//  Copyright © 2015年 HuiDong. All rights reserved.
//

#import "InstallationPointsTableViewController.h"
#import "InstallationPointTableViewCell.h"
#import "ShowMore.h"
#import "AFNetworking.h"
#import "Constant.h"
#import "AppDelegate.h"

@interface InstallationPointsTableViewController ()

@property (nonatomic, strong) NSMutableArray *arrayAll;
@property (nonatomic, assign) int a;

- (IBAction)back:(id)sender;
@end

@implementation InstallationPointsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //页面的显示数据
    //网络请求
    AFHTTPRequestOperationManager *request = [AFHTTPRequestOperationManager manager];
    request.responseSerializer = [AFJSONResponseSerializer serializer];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    dic[@"token"] = tempAppDelegate.token;
    dic[@"pageSize"] = @10;
    dic[@"pageIndex"] = [NSNumber numberWithInt:(++self.a)];
    
    [request POST:[NSString stringWithFormat:@"%@%@",url2,@"GetInstallPoint"] parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if ([[NSString stringWithFormat:@"%@",dic[@"Status"]] isEqualToString:@"0"]) {
            NSDictionary *dic1 = dic[@"Data"];
            NSArray *array = dic1[@"list"];
            self.arrayAll = [NSMutableArray arrayWithArray:array];
            [self.tableView reloadData];
        }else
        {
            [[[UIAlertView alloc] initWithTitle:@"提示" message:dic[@"Message"] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"系统网络申请错误!");
    }];
    
    //footView的加载更多的显示
    ShowMore *showMore = [ShowMore footView];
    
    __weak InstallationPointsTableViewController *tempController = self;
    
    ShowMoreBlock block = ^{
        //网络请求
        AFHTTPRequestOperationManager *request = [AFHTTPRequestOperationManager manager];
        request.responseSerializer = [AFJSONResponseSerializer serializer];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        dic[@"token"] = tempAppDelegate.token;
        dic[@"pageSize"] = @10;
        dic[@"pageIndex"] = [NSNumber numberWithInt:(++self.a)];
        
        [request POST:[NSString stringWithFormat:@"%@%@",url2,@"GetInstallPoint"] parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dic = (NSDictionary *)responseObject;
            if ([[NSString stringWithFormat:@"%@",dic[@"Status"]] isEqualToString:@"0"]) {
                NSDictionary *dic1 = dic[@"Data"];
                NSArray *array = dic1[@"list"];
                [tempController.arrayAll addObjectsFromArray:array];
                [tempController.tableView reloadData];;
            }else
            {
                [[[UIAlertView alloc] initWithTitle:@"提示" message:dic[@"Message"] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"系统网络申请错误!");
        }];
        
    };
    
    showMore.block = block;
    self.tableView.tableFooterView = showMore;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//数组懒加载

- (NSMutableArray *)arrayAll
{
    if (!_arrayAll) {
        _arrayAll = [NSMutableArray array];
    }
    return _arrayAll;
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


//设置数据源方法-行数和行内内容
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayAll.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InstallationPointTableViewCell *cell = [InstallationPointTableViewCell InstallationPointsWithTableView:tableView];
    cell.dic = self.arrayAll[indexPath.row];
    return cell;
}



//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}



//隐藏状态栏
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return YES;
}


@end
